﻿
/*============== simpleIRC - http://matthias.van-eeghem.com/ ==============*
 *                                                                         *
 *  File: Config.cs                                                   *
 *  Project: simpleIRC                                                     *
 *  Author(s): Matthias Van Eeghem <matthias@van-eeghem.com>               *
 *                                                                         *
 * ========================================================================*
 *                                                                         *
 *  This program is free software: you can redistribute it and/or modify   *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation, either version 3 of the License, or      *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *  This program is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
 *                                                                         *
===========================================================================*/

using System.Collections.Generic;
using System.Windows;
using System.Xml;
using System.Xml.XPath;

namespace simpleIRC.UI
{
    /// <summary>
    /// This class holds all functions related to reading and saving settings.xml
    /// </summary>
    
    public class Config
    {
        /// <summary>
        /// This Dictionary holds all application settings
        /// </summary>
        public Dictionary<string, string> Settings = new Dictionary<string, string>();

        /// <summary>
        /// This variable holds a reference to the MainWindow class
        /// </summary>
        MainWindow mainWindow = null;

        /// <summary>
        /// The constructor of the Config class
        /// </summary>

        public Config(MainWindow parentWindow)
        {
            mainWindow = parentWindow;

            LoadConfig();
        }

        /// <summary>
        /// This function returns a value from the config
        /// </summary>
        /// <param name="key">The key as a string</param>
        /// <returns>Returns the value</returns>

        public string GetConfigValue(string key)
        {
            XPathDocument SettingsDocument = new XPathDocument("settings.xml");
            XPathNavigator SettingsNavigator = SettingsDocument.CreateNavigator();

            XPathExpression XmlExpression = SettingsNavigator.Compile("/Configuration/" + key);
            XPathNodeIterator XmlIterator = SettingsNavigator.Select(XmlExpression);

            XmlIterator.MoveNext();
            XPathNavigator NavigatorClone = XmlIterator.Current.Clone();

            return NavigatorClone.Value.ToString();
        }

        /// <summary>
        /// This function sets a value in the settings.xml file
        /// </summary>
        /// <param name="key">The key as a string</param>
        /// <param name="value">The value to set it</param>

        public void SetConfigValue(string key, string value)
        {
            XmlDocument SettingsDocument = new XmlDocument();
            SettingsDocument.Load("settings.xml");
            SettingsDocument.DocumentElement.SelectSingleNode(key).InnerText = value;
            SettingsDocument.Save("settings.xml");
            SettingsDocument.RemoveAll();
        }

        /// <summary>
        /// This method loads all the configuration settings from settings.xml into the Settings dictionary
        /// </summary>

        public void LoadConfig()
        {
            Settings["Timestamp"] = this.GetConfigValue("Timestamp");
            Settings["UserListVisible"] = this.GetConfigValue("UserListVisible");
            Settings["ChannelListVisible"] = this.GetConfigValue("ChannelListVisible");
            Settings["SortChannels"] = this.GetConfigValue("SortChannels");
            Settings["TotalOnlineTime"] = this.GetConfigValue("TotalOnlineTime");

            // Enable or disable the UsersTreeview
            if (Settings["UserListVisible"] == "true")
            {
                mainWindow.UsersTreeView.Visibility = Visibility.Visible;
                mainWindow.UsersTreeView.Width = 100;
            }
            else
            {
                mainWindow.UsersTreeView.Visibility = Visibility.Hidden;
                mainWindow.UsersTreeView.Width = 0;
            }

            // Enable or disable the ChannelsTreeview
            if (Settings["ChannelListVisible"] == "true")
            {
                mainWindow.ChannelsTreeView.Visibility = Visibility.Visible;
                mainWindow.ChannelsTreeView.Width = 130;
            }
            else
            {
                mainWindow.ChannelsTreeView.Visibility = Visibility.Hidden;
                mainWindow.ChannelsTreeView.Width = 0;
            }
        }
    }
}
